package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.PartyRoomResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import kotlinx.coroutines.coroutineScope

/**
 * LiveFollowRoomViewModel
 *
 * @author Created by 天晴 on 2024/12/26/021 11:57
 */
class LiveFollowRoomViewModel : BaseViewModel() {

    var pageNumber = 0

    val mListLiveData = MutableLiveData<ResultState<ListResp<Any>>>()

    val livesLiveData = MutableLiveData<ResultState<ListResp<Any>>>()
    val recommendData = MutableLiveData<ResultState<List<DisplayUserBean>>>()

    val notHaveData = MutableLiveData<Boolean>()
    fun getLiveData() {
        request({
            if (pageNumber == 0) {
                coroutineScope {
                    val followResp = homeService.getRoomFollowed(pageNumber)
                    val followSize = followResp.result?.list?.size ?: 0
                    val list = arrayListOf<Any>()
                    if (followSize <= 4) {
                        followResp.result?.list?.let {
                            list.addAll(it)
                        }
                        val recommendResp = homeService.getRoomRecommend()
                        recommendResp.result?.let {
                            if (followSize == 0 && it.size != 0) {
                                list.add("")
                            }
                            //当没有关注也没有推荐的时候展示整页的空布局
                            if (followSize == 0 && it.size == 0) {
                                notHaveData.value = true
                            }
                            recommendData.value = ResultState.onAppSuccess(it)
                        }
                    } else {
                        followResp.result?.list?.let {
                            list.addAll(it)
                        }
                        recommendData.value = ResultState.onAppSuccess(arrayListOf())
                    }

                    followResp.new(list)
                }
            } else {
                homeService.getRoomFollowed(pageNumber).let { it.new(it.result?.list) }
            }
        }, livesLiveData)
    }

    val mPartyRoomLiveData = MutableLiveData<ResultState<PartyRoomResp>>()
    fun getPartyRoom() {
        request({ liveService.getPartyRoom() }, mPartyRoomLiveData)
    }
}